const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');

Page({
  data: {
    isloading: true,
    title:'',
    day:'',
    adress:'',
    people:'',
    fee:'',
    detail:'',
    bottomLift: 0,
    infodata:'',
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
  },
  onLoad() {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
  },
  onShow(){
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin()
    }else{
      this.setData({
        page:1,
        pageSize:10,
        isEnd:false,
        contentlistdata:[],
      })
      this.info()
      this.userTimeList()
    }
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          infodata:res.data.baseData
        })
      }
    })
  },
  userTimeList(){
    Controller.userTimeList({page:this.data.page,pageSize:this.data.pageSize}).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        this.setData({
          contentlistdata:this.data.contentlistdata.concat(res.data.baseData.list),
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.userTimeList()
    }
    
  },
  changeinput(e){
    var name=e.currentTarget.dataset.name;
    this.setData({
      [name]:e.detail.value
    })
  },
  comfirm(e){
    if(!this.data.title){
      wx.showToast({
        title: '请输入活动标题',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(!this.data.adress){
      wx.showToast({
        title: '请输入活动地址',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(!this.data.day){
      wx.showToast({
        title: '请输入活动天数',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(!this.data.people){
      wx.showToast({
        title: '请输入活动人数',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(!this.data.fee){
      wx.showToast({
        title: '请简要费用说明',
        icon: 'none',
        duration: 1200
      });
      return
    }
    if(!this.data.detail){
      wx.showToast({
        title: '请简要介绍活动详情',
        icon: 'none',
        duration: 1200
      });
      return
    }
    var data={
      title:this.data.title,
      location:this.data.title,
      days:this.data.day,
      personNum:this.data.people,
      expense:this.data.fee,
      content:this.data.detail
    };
    Controller.publish(data).then(res=>{
      wx.showToast({
        title: '提交成功！',
        icon: 'none',
        duration: 1200
      });
      this.setData({
        title:'',
        day:'',
        adress:'',
        people:'',
        fee:'',
        detail:'',
      })
    })
  },
  help(){
    wx.navigateTo({
      url:"/pages/pulish/help"
    });
  },
  golink(e){
    if(e.currentTarget.dataset.type=="swith"){
      wx.switchTab({
        url:e.currentTarget.dataset.url
      });
    }else{
      wx.navigateTo({
        url:e.currentTarget.dataset.url
      });
    }
    
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },
})